package kzy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import kzy.common.BaseContext;
import kzy.common.R;
import kzy.entiy.AddressBook;
import kzy.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 大溪地
 * @create 2023年05月30日20:21
 * 配送地址
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

	@Autowired
	AddressBookService addressBookService;

	/**
	 * 添加地址
	 * @param request
	 * @param addressBook
	 * @return
	 */
	@PostMapping()
	 public R<AddressBook> addressBook(HttpServletRequest request ,@RequestBody AddressBook addressBook){
//		获取session里面的id
		Long empId = (Long) request.getSession().getAttribute("employee");
		BaseContext.setCurrentId(empId);
//		设置用户id
		addressBook.setUserId(empId);
//		报错
		addressBookService.save(addressBook);
		 return  R.success(addressBook);
	}


	/**
	 * 获取通讯录列表
	 * @return
	 */
	@GetMapping("/list")
	public R<List<AddressBook>>  getAddressBookList(HttpServletRequest request) {
		Long empId = (Long) request.getSession().getAttribute("employee");
		LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AddressBook::getUserId,empId);
		List<AddressBook> list = addressBookService.list(queryWrapper);
		return R.success(list);
	}


	/**
	 * default
	 * 设置为默认地址
	 */
	@PutMapping("/default")
	public R<AddressBook> soToDefault(HttpServletRequest request ,@RequestBody AddressBook addressBook){
//		获取session里面的id
		Long empId = (Long) request.getSession().getAttribute("employee");
		BaseContext.setCurrentId(empId);
//		创建更新条件构造器
		LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
		wrapper.eq(AddressBook::getUserId, empId);
//		把所以符合条件的先全部设置为0 不是默认的 因为默认的只能有一个
		wrapper.set(AddressBook::getIsDefault, 0);
		addressBookService.update(wrapper);
//		根据id 查询 那个要设置默认值的地址的信息
		addressBook.setIsDefault(1);
		addressBookService.updateById(addressBook);
		AddressBook addressBook2 = addressBookService.getById(addressBook.getId());
		return R.success(addressBook2);
	}

	/**
	 * 根据id查询地址
	 */
	@GetMapping("/{id}")
	public R get(@PathVariable Long id) {
		AddressBook addressBook = addressBookService.getById(id);
		if (addressBook != null) {
			return R.success(addressBook);
		} else {
			return R.error("没有找到该对象");
		}
	}

	/**
	 * 查询默认地址
	 */
	@GetMapping("default")
	public R<AddressBook> getDefault() {
		LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
		queryWrapper.eq(AddressBook::getIsDefault, 1);

		//SQL:select * from address_book where user_id = ? and is_default = 1
		AddressBook addressBook = addressBookService.getOne(queryWrapper);

		if (null == addressBook) {
			return R.error("没有找到该对象");
		} else {
			return R.success(addressBook);
		}
	}
}
